package Ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

public abstract class AbsPersonalPage implements AjaxPage {
	protected boolean bCreate = false;
	protected boolean bSubmit = false;
	protected boolean bDelete = false;
	protected int     nModify = 0;
	protected int mResumeId = 0;
	protected ResultSet rs = null;
	protected int mId = 0;

	protected String mStrSubClassName = "";

	public AbsPersonalPage(String resumeId, HttpServletRequest request) {
		String str = null;

		if ((str = request.getParameter("create")) != null) {
			bCreate = str.equalsIgnoreCase("1");
		} else if ((str = request.getParameter("submit")) != null) {
			bSubmit = str.equalsIgnoreCase("1");

			if ((str = request.getParameter("modifid")) != null) {
				nModify = Integer.valueOf(str);
			}
		} else if ((str = request.getParameter("deletesubmit")) != null) {
			bDelete = str.equalsIgnoreCase("1");
		} else if ((str = request.getParameter("modifid")) != null) {
			nModify = Integer.valueOf(str);
		}

		if ((str = request.getParameter("deleteid")) != null) {
			mId = Integer.valueOf(str);
		}

		if (resumeId != null) {
			mResumeId = Integer.valueOf(resumeId);// id;
		}
	}

	@Override
	public void printXML(PrintWriter writer) throws SQLException {
		if (bCreate) {
			try {
				writeWithCreate(writer);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if(nModify > 0) {
			writeModify(writer);
		} else {
			writeWithoutCreate(writer);
		}
	}

	@Override
	public void printHtml(PrintWriter writer) {
		writer.print(mStrSubClassName + (nModify > 0 ? "(\"modified\")" : "(\"created\")"));
	}

	@Override
	public void printDel(PrintWriter writer) {
		writer.print(mStrSubClassName + "(\"deleted\")");
	}

	protected abstract void writeWithoutCreate(PrintWriter writer)  throws SQLException;

	protected abstract void writeWithCreate(PrintWriter writer) throws IOException;

	protected void writeModify(PrintWriter writer) throws SQLException {
	}
}
